using System.Collections.Generic;
using Game.Const;
using Game.Core;
using Game.Main.Model;
using Game.Manager;
using Game.XH;
using SimpleJSON;
using UnityEngine;

namespace Game.Component
{
	public class CarBrushItem : BaseItem
	{
		public enum Anim
		{
			Static = 0,
			Up = 1,
			Down = 2
		}

		public enum State
		{
			Down = 0,
			UpStatic = 1,
			UpRoll = 2
		}

		private float DOWN_Y = -4f;

		private float UP_Y = 0.2f;

		private float MAX_SPEED = 180f;

		private float ACCELERATION = 90f;

		private List<Transform> brushes;

		private List<Transform> balls = new List<Transform>();

		private float curY;

		private float ballAngle;

		private float speed;

		private Anim animState;

		private State state;

		private Transform bubbleEfc;

		public override void Init(ItemArchiveData data)
		{
			base.Init(data);
			int childCount = model.childCount;
			brushes = new List<Transform>();
			for (int i = 0; i < childCount; i++)
			{
				Transform child = model.GetChild(i);
				if (child.name.Equals("Brush"))
				{
					InitBrushZ(child);
					brushes.Add(child);
				}
			}
			SetState((State)data.state);
		}

		protected override void LateUpdate()
		{
			if (state == State.UpRoll)
			{
				if (speed < MAX_SPEED)
				{
					speed += ACCELERATION * Time.deltaTime;
					if (speed > MAX_SPEED)
					{
						speed = MAX_SPEED;
					}
				}
				ballAngle += Time.deltaTime * speed;
				SetBallsRotation(ballAngle);
			}
			if (animState == Anim.Static)
			{
				return;
			}
			if (animState == Anim.Up)
			{
				curY += Time.deltaTime * 4f;
				if (curY >= UP_Y)
				{
					SetBrushesY(UP_Y);
					animState = Anim.Static;
				}
				else
				{
					SetBrushesY(curY);
				}
			}
			else
			{
				if (animState != Anim.Down)
				{
					return;
				}
				if (speed > 0f)
				{
					speed -= ACCELERATION * Time.deltaTime;
					if (speed < 0f)
					{
						speed = 0f;
						RemoveAudioPlayer();
					}
					ballAngle += Time.deltaTime * speed;
					SetBallsRotation(ballAngle);
				}
				else
				{
					curY -= Time.deltaTime * 4f;
					if (curY <= DOWN_Y)
					{
						SetBrushesY(DOWN_Y);
						animState = Anim.Static;
						SetModelCollSize(0);
						CheckCarIsClean();
					}
					else
					{
						SetBrushesY(curY);
					}
				}
			}
		}

		private void CheckCarIsClean()
		{
			RaycastHit2D[] array = Physics2D.BoxCastAll(base.transform.position + new Vector3(-0.34f, 2.65f), new Vector2(5.325f, 4.29f), 0f, Vector2.zero, 15f, 512);
			for (int i = 0; i < array.Length; i++)
			{
				RaycastHit2D raycastHit2D = array[i];
				if (raycastHit2D.collider != null)
				{
					BaseItem component = raycastHit2D.collider.transform.parent.GetComponent<BaseItem>();
					if (component != null && component.itemData.scriptType == 24)
					{
						Singleton<AudioManager>.Instance.PlaySound("sound_blink");
						Singleton<EffectManager>.Instance.ShowEffect(PathConst.EFFECT + "efc_blink_1", component.transform.position + new Vector3(0f, 1.5f, -0.0004f), 3f);
					}
				}
			}
		}

		private void SetState(State val)
		{
			state = val;
			if (state == State.Down)
			{
				SetBrushesY(DOWN_Y);
				SetModelCollSize(0);
				return;
			}
			if (state == State.UpRoll)
			{
				PlayBubbleEffect();
			}
			SetBrushesY(UP_Y);
			SetModelCollSize(1);
		}

		private void SetBrushesY(float y)
		{
			for (int i = 0; i < brushes.Count; i++)
			{
				Transform trans = brushes[i];
				trans.SetLocalY(y);
			}
		}

		private void SetBallsRotation(float angle)
		{
			for (int i = 0; i < balls.Count; i++)
			{
				balls[i].eulerAngles = new Vector3(0f, 0f, angle);
			}
		}

		private void PlayBubbleEffect()
		{
			if (string.IsNullOrEmpty(base.itemData.dragEfc))
			{
				return;
			}
			if (bubbleEfc == null)
			{
				Transform transform = model.Find("Efc");
				if (transform != null)
				{
					transform.SetLocalZ(-0.00024999998f);
					bubbleEfc = Singleton<GamePoolManager>.Instance.Spawn(PathConst.EFFECT + base.itemData.dragEfc);
					if (bubbleEfc != null)
					{
						bubbleEfc.SetParent(transform);
						bubbleEfc.localPosition = Vector3.zero;
						bubbleEfc.localScale = Vector3.one;
					}
				}
			}
			SetEffectLoop(bubbleEfc, true);
		}

		private void StopBubbleEffect()
		{
			SetEffectLoop(bubbleEfc, false);
		}

		public override void QuicklyTouch(TouchEventData touchEventData)
		{
			base.QuicklyTouch(touchEventData);
			if (animState == Anim.Static)
			{
				if (state == State.Down)
				{
					animState = Anim.Up;
					state = State.UpStatic;
					curY = DOWN_Y;
					SetModelCollSize(1);
					Singleton<AudioManager>.Instance.PlayAudio("sound_car_brush_1", false, GetAudioPlayer());
				}
				else if (state == State.UpStatic)
				{
					state = State.UpRoll;
					PlayBubbleEffect();
					Singleton<AudioManager>.Instance.PlayAudio("sound_car_brush_2", true, GetAudioPlayer());
				}
				else if (state == State.UpRoll)
				{
					animState = Anim.Down;
					state = State.Down;
					curY = UP_Y;
					StopBubbleEffect();
				}
			}
		}

		private void SetModelCollSize(int type)
		{
			if (type == 1)
			{
				coll.offset = new Vector2(-0.34f, 2.57f);
				coll.size = new Vector2(5.89f, 4.8f);
			}
			else
			{
				coll.offset = new Vector2(-0.34f, 0.57f);
				coll.size = new Vector2(5.89f, 0.81f);
			}
		}

		private void InitBrushZ(Transform brush)
		{
			int childCount = brush.childCount;
			for (int num = childCount - 1; num >= 0; num--)
			{
				Transform child = brush.GetChild(num);
				child.SetLocalZ((float)(num + 1) * -1E-05f);
				balls.Add(child);
			}
			brush.SetLocalZ((float)(childCount + 1) * 1E-05f);
		}

		public override void PackData(JSONObject obj)
		{
			base.PackData(obj);
			if (state != 0)
			{
				obj["33"] = (int)state;
			}
		}
	}
}
